package com.csu.cphone.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.exception.WebException;
import com.csu.cphone.constants.ErrorCode;
import com.csu.cphone.pojo.CustomerPojo;
import com.csu.cphone.pojo.ProducerPojo;
import com.csu.cphone.service.UserService;
import com.csu.cphone.vo.ResultVo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：cphone
 * @类名称：RecycleController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月4日 上午10:37:18
 * @company:步步高教育电子有限公司
 */
@Controller
@RequestMapping("/user")
@Api(value = "base", description = "基础信息")
public class UserController
{

    @Autowired
    private UserService userService;

    @ResponseBody
    @ApiOperation(value = "客户注册", notes = "校验注册参数，成功后返回用户信息")
    @RequestMapping(value = "customer/sign", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<CustomerPojo> customerSign(HttpServletRequest request,
            @ApiParam(value = "微信appid") @RequestParam(value = "wxAppid", required = true) String wxAppid,
            @ApiParam(value = "登录密码") @RequestParam(value = "password", required = true) String password,
            @ApiParam(value = "电话号码") @RequestParam(value = "number", required = true) String number,
            @ApiParam(value = "名称") @RequestParam(value = "name", required = false) String name) throws WebException
    {
        if (wxAppid == null || number == null || password == null || "".equals(wxAppid.trim())
                || "".equals(number.trim()) || "".equals(password.trim()))
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        ResultVo<CustomerPojo> result = new ResultVo<CustomerPojo>();

        // 登录操作
        CustomerPojo customer = userService.customerSign(wxAppid, number, password, name);

        result.setData(customer);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "客户登录", notes = "登录校验，成功后返回用户信息")
    @RequestMapping(value = "customer/login", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<CustomerPojo> customerLogin(HttpServletRequest request,
            @ApiParam(value = "微信appid") @RequestParam(value = "wxAppid", required = false) String wxAppid,
            @ApiParam(value = "登录密码") @RequestParam(value = "password", required = true) String password,
            @ApiParam(value = "电话号码") @RequestParam(value = "number", required = false) String number)
            throws WebException
    {
        if (wxAppid == null && number == null)
        {
            // 微信id和电话号码不能同时为空
            throw new WebException(ErrorCode.LoginParamErrorCode, ErrorCode.LoginParamErrorMessgae);
        }
        if (password == null)
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        ResultVo<CustomerPojo> result = new ResultVo<CustomerPojo>();

        // 登录操作
        CustomerPojo customer = userService.customerLogin(wxAppid, number, password);

        result.setData(customer);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "服务方登录", notes = "登录校验，成功后返回用户信息")
    @RequestMapping(value = "producer/login", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<ProducerPojo> producerLogin(HttpServletRequest request,
            @ApiParam(value = "登录密码") @RequestParam(value = "password", required = true) String password,
            @ApiParam(value = "电话号码") @RequestParam(value = "number", required = true) String number)
            throws WebException
    {
        if (number == null || password == null)
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        ResultVo<ProducerPojo> result = new ResultVo<ProducerPojo>();

        // 登录操作
        ProducerPojo producer = userService.producerLogin(number, password);

        result.setData(producer);
        return result;
    }
}
